package com.starchen.optimalselection.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.PageUtils;
import com.starchen.optimalselection.product.model.form.AttributeGroupForm;
import com.starchen.optimalselection.product.model.vo.GroupWithAttrVo;
import com.starchen.optimalselection.product.service.AttributeGroupService;
import com.starchen.optimalselection.common.service.AttributeService;
import com.starchen.optimalselection.common.service.CategoryService;
import com.starchen.optimalselection.common.mapper.AttributeGroupMapper;
import com.starchen.optimalselection.common.model.Attribute;
import com.starchen.optimalselection.common.model.AttributeGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author daxilong
 * @date 2024/11/27 20:09
 */

@Service
public class AttributeGroupServiceImpl extends ServiceImpl<AttributeGroupMapper, AttributeGroup> implements AttributeGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttributeGroupMapper attrGroupMapper;

    @Autowired
    private AttributeService attrService;

    @Override
    public ResultPage queryPage(AttributeGroupForm params) {
        String key =  params.getKeyword();
        QueryWrapper<AttributeGroup> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(key)){
            queryWrapper.like("name",key);
        }
        Long id = params.getCategoryId();
        if(id != null){
            queryWrapper.eq("category_id",id);
        }

        IPage<AttributeGroup> page = baseMapper.selectPage(PageUtils.getPage(params.getPageIndex(),params.getPageSize()), queryWrapper);
        return new ResultPage(page);
    }

    @Override
    public List<AttributeGroup> getGroupByCate(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",id);
        List<AttributeGroup> groupList = baseMapper.selectList(queryWrapper);
        return groupList;
    }

    @Override
    public List<AttributeGroup> getAllGroup(){
        List<AttributeGroup> groupList = baseMapper.selectList(null);
        return groupList;
    }

    @Override
    public AttributeGroup getAttrGroupInfo(Long attrGroupId) {
        AttributeGroup attrGroup = attrGroupMapper.selectById(attrGroupId);

        long cateId  = attrGroup.getCategoryId();
        Long[] path = categoryService.getCategoryPath(cateId);
        attrGroup.setCategoryPath(path);

        return attrGroup;
    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     */
    @Override
    public List<GroupWithAttrVo> getGroupWithAttrs(Long categoryId) {
        //1、查询分组信息
        QueryWrapper<AttributeGroup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id", categoryId);
        
        List<AttributeGroup> attrGroupEntities = this.list(queryWrapper);
        //2、查询所有属性
        List<GroupWithAttrVo> collect = attrGroupEntities.stream().map(group -> {
            GroupWithAttrVo attrsVo = new GroupWithAttrVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<Attribute> attrs = attrService.getListByGroup(attrsVo.getId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

}
